﻿using System;
using System.ServiceModel;
using System.Windows;
using System.Windows.Input;
using LoginUser.PopUpWindows;
using Sync.LMS;
using Sync.Log;
using Sync.SessionService;
using Sync.User;

namespace LoginUser
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        public static SplashUtilities Utilities = new SplashUtilities();
        

        public MainWindow()
        {
            InitializeComponent();
            ResizeMode = ResizeMode.CanMinimize;
            Preferences.UseFile(@"..\..\","Setup.ini");
            
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            //Uses the setup.ini file
            try
            {
                Preferences.IniConSource.AddConfig("User");
            }
            catch (Exception)
            {
                FileLog.LogMessage("[User] Already Added in Sync.ini", LogType.Warning);
            }
            if (!Preferences.IniConSource.Configs["User"].Contains("UserID"))
                Preferences.IniConSource.Configs["User"].Set("UserID", "");

            //Checks to see if there is nothing in the Alias keyword UserID and sets the nputUsername Field to what ever is in the file.
            if (!String.IsNullOrEmpty(Preferences.IniConSource.Configs["User"].GetString("UserID")))
            {
                InputUserName.Text = Preferences.IniConSource.Configs["User"].GetString("UserID");
                RememberMeCheckBox.IsChecked = true;
            }
        }

        private void Remember_Click(object sender, RoutedEventArgs e)
        {
            //checks to see if the checkbox is checked. If it is writes the username under User Alias under key UserID to the Setup.ini file.
            if (RememberMeCheckBox.IsChecked != null && RememberMeCheckBox.IsChecked == true)
            {
                Preferences.IniConSource.Configs["User"].Set("UserID", InputUserName.Text);
                Preferences.IniConSource.Save(Preferences.FullFilePath);
            }
            else
            {
                Preferences.IniConSource.Configs["User"].Set("UserID", "");
                Preferences.IniConSource.Save(Preferences.FullFilePath);
            }
        }

        private void Go_Click(object sender, RoutedEventArgs e)
        {
            LMSServiceUsage.LogIn(InputUserName.Text, InputPassword.Password);
            Visibility = Visibility.Hidden;
            Utilities.ShowDialog();
        }

        private void InputPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key != Key.Return) return;
            Go_Click(sender, e);
        }

        
    }
}